#!/usr/bin/ruby
require 'rubygems'
require 'bit-struct'
require 'zlib'
require 'stringio'
require 'RMagick'
require 'rvg/rvg'
include Magick





class Header < BitStruct
  unsigned :size, 16, "Size"
  unsigned :id, 16 , "ID"
end



class Style < BitStruct
    unsigned :version, 16
    unsigned :uncompressed, 32
    unsigned :compressed, 32
end


class TheString < BitStruct
  rest :text
end


class StdString < BitStruct
  unsigned :size, 32
end


class DynamicColorManager < BitStruct
  unsigned :count, 32
end


class MyInteger < BitStruct
  unsigned :value, 32
end

class Wall < BitStruct
  float :x, 32
  float :y, 32
  float :z, 32
  float :angle, 32
  float :width, 32
  float :height, 32
end

class Box < BitStruct
  float :x, 32
  float :y, 32
  float :z, 32
  float :angle, 32
  float :width, 32
  float :depth, 32
  float :height, 32
  unsigned :state, 8
end

class Pyramid < BitStruct
  float :x, 32
  float :y, 32
  float :z, 32
  float :angle, 32
  float :width, 32
  float :depth, 32
  float :height, 32
  unsigned :state, 8
end

class Base < BitStruct
  unsigned :team, 16
  float :x, 32
  float :y, 32
  float :z, 32
  float :angle, 32
  float :width, 32
  float :depth, 32
  float :height, 32
  unsigned :state, 8
end

class Teleporter < BitStruct
  
  float :x, 32
  float :y, 32
  float :z, 32
  float :angle, 32
  float :width, 32
  float :depth, 32
  float :height, 32
  float :border, 32
  unsigned :horizontal, 8
  unsigned :state, 8
end

#class DynamicColor < BitStruct
#  vector  :v, Vector, "a vector"
#end

def l(data)
  #p data
end

def map_png(map)

rvg = RVG.new(500, 500).viewbox(-250,-250,500,500) do |canvas|

io = StringIO.new map

  header = Header.new io.read(Header.round_byte_length)
  l header
  body = Style.new io.read(Style.round_byte_length)
  l body
  data = io.read(body.compressed).to_s
  data = Zlib::Inflate.new().inflate(data)
 

  if data.length == body.uncompressed
    l "Uncompressing OK"
  else
    l "Uncompressing Error"
    return nil
  end
  #p data
  data = StringIO.new data
  
  
  # data pobrana przez read jest network endian i w sumie nie powinna byc mapowana na liczby ale zero zawsze jest zero
  #DynamicColor
  dynamicColorCount = data.read(4).to_i
  if dynamicColorCount != 0: puts "DynamicColor not supported"; return nil end
  #TextureMatrix
  textureMatrixCount = data.read(4).to_i
  if textureMatrixCount != 0: puts "TextureMatrix not supported"; return nil end
  #BzMaterial
  bzMaterialCount = data.read(4).to_i
  if bzMaterialCount != 0: puts "BzMaterial not supported"; return nil end
  #PhysicsDriver
  physicsDriverCount = data.read(4).to_i
  if physicsDriverCount != 0: puts "PhysicsDriver not supported"; return nil end
  #MeshTransform
  meshTransformCount = data.read(4).to_i
  if meshTransformCount != 0: puts "MeshTransform not supported"; return nil end
  
  groupDefinitionLen =  data.read(4).to_i
  if groupDefinitionLen != 0: puts "GroupDefinitionName not supported"; return nil end

  
  

  canvas.rect(10,10,-5,-5).styles(:stroke=>'red')
  wallCount = (MyInteger.new data.read(MyInteger.round_byte_length)).value
  wallCount.times do
    wall = (Wall.new data.read(Wall.round_byte_length))
    l wall
    canvas.rect(wall.width,1,-wall.width/2,1).translate(-wall.x/2,-wall.y/2).rotate((wall.angle * 180/Math::PI)-90)
  end
  boxCount = (MyInteger.new data.read(MyInteger.round_byte_length)).value
  boxCount.times do
    box = (Box.new data.read(Box.round_byte_length))
    l box
    canvas.rect(box.width,box.depth,-box.width/2,-box.depth/2).translate(-box.y/2,-box.x/2).rotate((-box.angle * 180/Math::PI)).styles(:stroke=>'#600000', :fill=>'#600000')
  end
  pyrCount = (MyInteger.new data.read(MyInteger.round_byte_length)).value
  pyrCount.times do
    pyr = (Pyramid.new data.read(Pyramid.round_byte_length))
    l pyr
    canvas.rect(pyr.width,pyr.depth,-pyr.width/2,-pyr.depth/2).translate(-pyr.y/2,-pyr.x/2).rotate((-pyr.angle * 180/Math::PI)).styles(:stroke=>'#000080', :fill=>'#000080')
  end
  baseCount = (MyInteger.new data.read(MyInteger.round_byte_length)).value
  baseCount.times do
    pyr = (Base.new data.read(Base.round_byte_length))
    l pyr
  end
  teleCount = (MyInteger.new data.read(MyInteger.round_byte_length)).value
  teleCount.times do
    nameSize = (MyInteger.new data.read(MyInteger.round_byte_length)).value
    data.read(nameSize) #skip name
    pyr = (Teleporter.new data.read(Teleporter.round_byte_length))
    l pyr
    canvas.rect(pyr.width,pyr.depth,-pyr.width/2,-pyr.depth/2).translate(-pyr.y/2,-pyr.x/2).rotate((-pyr.angle * 180/Math::PI)-90).styles(:stroke=>'#CC6600', :fill=>'#CC6600')
  end
  
  
 
  

  ender = Header.new io.read(Header.round_byte_length)
  l ender
  io.close
end #canvas
 
img = rvg.draw
img.format = "png"
return img.to_blob

end



